package controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import dao.CommonDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import socket.Server;
import util.HttpUtil;

@Controller
@RequestMapping("/common")
public class CommonController extends BaseController {

	@Resource
	private CommonDao commonDao;

	
	/**
	 * 获取设备状态
	 * {id:"123"} id:设备id
	 * 
	 */
	@RequestMapping("/getDeviceStatus")
	public String getDeviceStatus(HttpServletRequest request, HttpServletResponse response) {
		try {
			String data = request.getReader().readLine();
			JSONObject dataJson = JSONObject.fromObject(data);
			JSONArray ids=dataJson.getJSONArray("ids");//设备id
			JSONArray deviceJsonObj=Server.getDeviceStatus(ids);
			if(deviceJsonObj!=null){
				outJson(response, deviceJsonObj.toString());
			}else{
				outJson(response, "{\"result\":\"0\"}");
			}
		} catch (Exception e) {
			outJson(response, "{\"result\":\"0\"}");
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 执行命令是否成功
	 * {id:"aefcf13jj"} id:命令id
	 * 
	 */
	@RequestMapping("/SendCommandIfSuccess")
	public String SendCommandIfSuccess(HttpServletRequest request, HttpServletResponse response) {
		try {
			String data = request.getReader().readLine();
			JSONObject dataJson = JSONObject.fromObject(data);
			String id=dataJson.getString("id");//命令id
			if(Server.SendCommandIfSuccess(id)){
				outJson(response, "{\"result\":\"1\"}");
			}else{
				outJson(response, "{\"result\":\"0\"}");	
			}
		} catch (Exception e) {
			outJson(response, "{\"result\":\"0\"}");
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 发送命令给设备
	 * 
	 * 
	 	{
			id:”11330”,
			did:”123”,
			command:0
			type:”control”
		}
		id:消息ID 服务端随机生成
		did:设备id
		command:命令 0关闭，1打开
		type:包类型 control控制包
	 * 
	 */
	@RequestMapping("/SendCommand")
	public String SendCommand(HttpServletRequest request, HttpServletResponse response) {
		try {
			String data = request.getReader().readLine();
			JSONObject dataJson = JSONObject.fromObject(data);
			if(Server.SendCommandThread(dataJson)){
				outJson(response, "{\"result\":\"1\"}");
			}else{
				outJson(response, "{\"result\":\"0\"}");
			}
			
		} catch (Exception e) {
			outJson(response, "{\"result\":\"0\"}");
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param request
	 * @param response
	 * @return 执行sql返回json
	 */
	@RequestMapping("/listSql")
	public String listSql(HttpServletRequest request, HttpServletResponse response) {
		try {
			String data = request.getReader().readLine();
			JSONObject dataJson = JSONObject.fromObject(data);
			outJson(response, commonDao.list(dataJson.getString("sql")).toString());
		} catch (Exception e) {
			outJson(response, "{\"result\":\"0\"}");
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param request
	 * @param response
	 * @return 执行sql
	 */
	@RequestMapping("/executeSql")
	public String executeSql(HttpServletRequest request, HttpServletResponse response) {
		try {
			String data = request.getReader().readLine();
			JSONObject dataJson = JSONObject.fromObject(data);
			String sql = dataJson.getString("sql");
			commonDao.execute(sql);
			outJson(response, "{\"result\":\"1\"}");
		} catch (Exception e) {
			outJson(response, "{\"result\":\"0\"}");
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param request
	 * @param response
	 * @return 往session里添加值
	 */
	@RequestMapping("/setSessionValue")
	public String setSessionValue(HttpServletRequest request, HttpServletResponse response) {
		try {
			String name = request.getParameter("name");
			String value = request.getParameter("value");
			request.getSession().setAttribute(name, value);
			outJson(response, "{\"result\":\"1\"}");
		} catch (Exception e) {
			outJson(response, "{\"result\":\"0\"}");
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param request
	 * @param response
	 * @return 获取session里的某个值
	 */
	@RequestMapping("/getSessionValue")
	public String getSessionValue(HttpServletRequest request, HttpServletResponse response) {
		try {
			String name = request.getParameter("name");
			outJson(response, "{\"value\":\"" + request.getSession().getAttribute(name) + "\"}");
		} catch (Exception e) {
			outJson(response, "{\"result\":\"0\"}");
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param request
	 * @param response
	 * @return 发送get请求
	 */
	@RequestMapping("/sendGetRequest")
	public String sendGetRequest(HttpServletRequest request, HttpServletResponse response) {
		try {
			String url = request.getParameter("url");
			outJson(response, HttpUtil.doGet(url));
		} catch (Exception e) {
			outJson(response, "{\"result\":\"0\"}");
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/test")
	public String test(HttpServletRequest request, HttpServletResponse response) {
		try {
			outJson(response, "{\"success\":\"success\"}");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
